﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace JiveMessenger.CustomControls
{
    public partial class FauxKeyboardControl : UserControl
    {
        public event EventHandler<EventArgs> ShowFauxKeyboardCompleted;
        public event EventHandler<EventArgs> HideFauxKeyboardCompleted;
        public bool ShowFauxKeyboard
        {
            get { return (bool)GetValue(ShowFauxKeyboardProperty); }
            set
            {

                SetValue(ShowFauxKeyboardProperty, value);
            }
        }



        public double FauxHeight
        {
            get { return (double)GetValue(FauxHeightProperty); }
            set { SetValue(FauxHeightProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Height.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FauxHeightProperty =
            DependencyProperty.Register("FauxHeight", typeof(double), typeof(FauxKeyboardControl), new PropertyMetadata(0.0));




        // Using a DependencyProperty as the backing store for ShowFauxKeyboard.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShowFauxKeyboardProperty =
            DependencyProperty.Register("ShowFauxKeyboard", typeof(bool), typeof(FauxKeyboardControl), new PropertyMetadata(false, new PropertyChangedCallback(ShowFauxKeyboardProperty_Callback)));



        public FauxKeyboardControl()
        {
            InitializeComponent();
        }

        private static void ShowFauxKeyboardProperty_Callback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            FauxKeyboardControl control = dependencyObject as FauxKeyboardControl;

            if ((bool)dependencyPropertyChangedEventArgs.NewValue == true)
            {
                control.FauxKeyboardGrid.Height = control.FauxHeight; ;
                //Storyboard sb = control.Resources["ShowFauxKeyboardAnim"] as Storyboard;
                //sb.Begin();

                //control.FauxKeyboardGrid.Animate(1, control.FauxHeight, "Grid.Height", 300, 100, null, () =>
                //    {
                //        if (control.ShowFauxKeyboardCompleted != null)
                //            control.ShowFauxKeyboardCompleted(control, null);
                //    });
                if (control.ShowFauxKeyboardCompleted != null)
                    control.ShowFauxKeyboardCompleted(control, null);

                
            }
            else
            {
                //control.FauxKeyboardGrid.Animate(340, 1, "(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)", 500, 0, null, () =>
                //{
                //    if (control.HideFauxKeyboardCompleted != null)
                //        control.HideFauxKeyboardCompleted(control, null);
                //});
                control.FauxKeyboardGrid.Height = 0;
                if (control.HideFauxKeyboardCompleted != null)
                    control.HideFauxKeyboardCompleted(control, null);
                //Storyboard sb = control.Resources["HideFauxKeyboardAnim"] as Storyboard;
                //sb.Begin();
            }
        }



        private void HideFauxKeyboardAnim_Completed(object sender, object e)
        {
            if (HideFauxKeyboardCompleted != null)
                HideFauxKeyboardCompleted(this, null);
        }

        private void ShowFauxKeyboardAnim_Completed(object sender, object e)
        {
            if (ShowFauxKeyboardCompleted != null)
                ShowFauxKeyboardCompleted(this, null);
        }

    }
}
